<?php
/**
 * Joomla! 1.5 component jhtraq
 *
 * @version $Id: view.html.php 2010-03-09 08:28:06 svn $
 * @author JHShop
 * @package Joomla
 * @subpackage jhtraq
 * @license GNU/GPL
 *
 * Traq type system for Joomla
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the jhtraq component
 */
class JhtraqViewReleases extends JView {
    function display($tpl = null) {

        if(JRequest::getVar('task') == 'download') {
            $this->download();
        }
        $model =& JhtraqHelper::getModel('jhtraq');
        $project = JRequest::getVar('project');
        $this->project = $model->getProjectBySlug($project);
        $db =& JFactory::getDBO();
        $db->setQuery("Select * from #__jhtraq_releases where published='1' and project_id='".$this->project->id."' order by released desc");
        $this->releases = $db->loadAssocList();

        parent::display($tpl);
    }

    function download() {
        $id = JRequest::getVar('download');
        $db =& JFactory::getDBO();
        $db->setQuery("Select * from #__jhtraq_releases where id='$id' and published='1'");
        $row = $db->loadObject();
        if(!$row->id)
            return;


        $jhshop = $row->jhshop_prod_id;
        $ambra = $row->ambrasubs_types;
        $link = $row->download_link;
        $user =& JFactory::getUser();

        $db->setQuery("Update #__jhtraq_releases set downloads=downloads+1 where id='$id'");
        $db->query();
        if($jhshop > 0) {

            $db->setQuery("Select keyword from #__jhs_url_alias where `query`='product_id=$jhshop' limit 1");
            $res = $db->loadResult();
            if($res) {
                $link = JRoute::_('index.php?option=com_jhshop&shoppage='.$res);
            }else {
                $link = JRoute::_('index.php?option=com_jhshop&route=product/product&product_id='.$jhshop);
            }



            if($user->id) {
                $db->setQuery("Select count(*) as cnt from #__jhs_order_download as d left join #__jhs_order as o on d.order_id=o.order_id where d.order_product_id='$jhshop' and o.customer_id='{$user->id}' and d.remaining > 0");
                $ct = $db->loadResult();
                if($ct) {
                    $link = JRoute::_('index.php?option=com_jhshop&route=account/download');
                }
            }
        }
        if($ambra != '' && $ambra != '0' && file_exists(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_ambrasubs' . DS . 'ambrasubs.php')) {
            $ambras = explode(",", $ambra);
            //either send to the my subscriptions page / to the order sybscription page!
            $subscribed = false;
            $userid = (int)$user->id;
            foreach($ambras as $type) {
                if($subscribed) continue;
                $isUser = AmbrasubsHelperSubscription::isUser( $userid, $type, '1', '1' );
                if (!is_object($isUser) || $isUser->status != '1') {
                    /* the user has NO active subscription, so do some kind of action */
                } else {
                    /* the user HAS an active subscription, so do something else */
                    $subscribed = true;
                }
            }
            if($subscribed){
                $link = JRoute::_('index.php?option=com_ambrasubs&view=subscriptions&layout=mine');
            }else{
                $link = JRoute::_('index.php?option=com_ambrasubs&view=subscriptions');
            }
        }
        //echo $ambra;
       // die($link);
        $mainframe =& JFactory::getApplication();
        $mainframe->redirect($link);
        $mainframe->close();

    }
}
?>